var express = require('express');
var router = express.Router();

const FoodService = require('../server/service/food.service.js');


/* GET food listing. */
router.post('/', function(req, res, next) {
   if(req.session.user){
       let merchantId = req.session.user._id;
       req.body.merchant = merchantId;
        console.log('[插入食物]',req.body)
        FoodService.insertFood(merchantId, req.body,function(doc){
            res.send(doc);
        });
    }else{
        res.send({status:false,message:'添加失败'});
    }
   

});
router.delete('/:_id', function(req, res, next) {
    console.log('[删除食物]',req.params._id)
    let merchantId = req.session.user._id;    
    FoodService.deleteFood(merchantId, req.params._id,function(doc){
        if(doc.status == true){
            res.send(doc);
        }else{
            doc.message='删除失败';
            res.send(doc);
        }
    });

});
router.put('/', function(req, res, next) {
    console.log('[修改食物]',req.body)
    let merchantId = req.session.user._id;    
    FoodService.updateFood(merchantId, req.body,function(doc){
            res.send(doc);
    });

});


/* GET food listing. */
router.get('/', function(req, res, next) {
    FoodService.findAll(function(doc){
        if(doc.status == true){
            if(doc.data == {}){
                doc.message='暂无数据';
            }
        }
        res.send(doc);
    });

});

router.get('/byM', function(req, res, next) {
    if(req.session.user){
        let merchantId = req.session.user._id;
        console.log('....................'+merchantId+'...........');
        FoodService.findByMId(merchantId,function(doc){
            console.log(doc);
            if(doc.status == true){
                if(doc.data == {}){
                    doc.message='暂无数据';
                }
            }
            res.send(doc);
        });
    }else{
        res.send({status:false,message:'暂无数据'});
    }
   

});
//根据foodID获取food信息
router.get('/:_id', function(req, res, next) {
    FoodService.findById(req.params._id,function(doc){
        if(doc.status == true){
            if(doc.data == {}){
                doc.message='暂无数据';
            }
        }
        res.send(doc);
    });
   
});
//根据foodList获取食品信息列表
router.post('/api/byFIds', function(req, res, next) {
    console.log('[Ids--获取food]',req.body);
    FoodService.findByIds(req.body,function(doc){
        if(doc.status == true){
            if(doc.data == {}){
                doc.message='暂无数据';
            }
        }
        res.send(doc);
    });
   
});

//根据商家ID获取食品信息
router.get('/api/byMId/:m_id', function(req, res, next) {
    FoodService.findByMId(req.params.m_id,function(doc){
        if(doc.status == true){
            if(doc.data == {}){
                doc.message='暂无数据';
            }
        }
        res.send(doc);
    });
   
});
//根据foodtype获取食品信息
router.get('/api/byType/:type_id', function(req, res, next) {
    FoodService.findByType(req.params.type_id,function(doc){
        if(doc.status == true){
            if(doc.data == {}){
                doc.message='暂无数据';
            }
        }
        res.send(doc);
    });
});
//根据商家id和商家foodtype来获取食品信息
router.get('/api/:m_id/:type_id', function(req, res, next) {
    FoodService.findByMIdandFTId(req.params.m_id,req.params.type_id,function(doc){
        if(doc.status == true){ 
            if(doc.data == {}){
                doc.message='暂无数据';
            }
        }
        res.send(doc);
    });
});
module.exports = router;